package com.gxa.controller;


import com.gxa.pojo.Alarm;
import com.gxa.pojo.AlarmVO;
import com.gxa.service.AlarmService;
import com.gxa.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "告警规则管理")
@RequestMapping("Alarm")
@Repository
public class AlarmController {
    @Autowired
    private AlarmService alarmService;

    @ApiOperation(value = "分页显示全部数据",httpMethod ="GET",response =ResponseData.class )
    @RequestMapping("/showAlarm")
    @ResponseBody
    public ResponseData showAlarm(@ApiParam(value = "Alarmvo对象",required = true)AlarmVO alarmVO){
        ResponseData responseData=new ResponseData();
        responseData.setCount(alarmService.getCount());
        List<Alarm> alarms=alarmService.showAlarm(alarmVO);
        responseData.setData(alarms);
        return responseData;
    }

    /**
     *
     * @return 查取的alarm数量和alarm数据
     */
    @ApiOperation(value = "根据条件查询",httpMethod ="GET",response = ResponseData.class)
    @RequestMapping("/selectByCondition")
    @ResponseBody
    public ResponseData selectByCondition(AlarmVO alarmVO){
        ResponseData responseData=new ResponseData();
        List<Alarm> alarms=alarmService.selectByCondition(alarmVO);
        for(Alarm alarm:alarms){
            System.out.println(alarm);
        }
        responseData.setData(alarms);
        responseData.setCount(alarmService.getCount());
        return responseData;
    }


    @ApiOperation(value = "添加数据",httpMethod ="GET",response = ResponseData.class)
    @RequestMapping("/addAlarm")
    @ResponseBody
    public ResponseData addAlarm(@ApiParam(value = "一个Alarm对象，必须有告警规则名称的属性",required = true)Alarm alarm){
        ResponseData responseData=new ResponseData();
        Boolean result=alarmService.addAlarm(alarm);
        responseData.setData(result);
        return responseData;
    }



    @ApiOperation(value = "通过告警规则名称删除数据",httpMethod ="GET",response = ResponseData.class)
    @RequestMapping("/deleteByAlarmRulesName")
    @ResponseBody
    public ResponseData deleteByAlarmRulesName(@ApiParam(value = "告警规则名称",required = true)String alarmRulesName){
        ResponseData responseData=new ResponseData();
        Boolean result=alarmService.deleteByAlarmRulesName(alarmRulesName);
        responseData.setData(result);
        return responseData;
    }


    @ApiOperation(value = "通过所有需要删除的告警规则的名称组成的数组，对告警规则进行删除处理",httpMethod ="POST",response = ResponseData.class)
    @PostMapping("/deleteByAlarmRulesNames")
    @ResponseBody
    public ResponseData deleteByAlarmRulesNames(@RequestParam List<String> alarmRulesNames){
        //统计删除的个数
        int count=0;
        ResponseData responseData=new ResponseData();
        for(String alarmRulesName:alarmRulesNames ){
            Boolean result=alarmService.deleteByAlarmRulesName(alarmRulesName);
            if(result==true){
                count++;
                responseData.setData("删除成功");
            }
        }
        responseData.setCount(count);

        return responseData;
    }


}
